package com.masterlu.leetcode.daily.stack.simple;

import java.util.Stack;

/**
 * 棒球比赛
 * https://leetcode-cn.com/problems/baseball-game/
 *
 * @Author：masterlu
 * @Date：2021/4/29 9:34 上午
 */
public class S682CalPoints {

    /**
     * 1。初始化stack，用于存储最终得分
     * 2。遍历ops数组，进行处理，把最终得分存入第一步栈中
     * 3.对第一步中栈元素求和
     * @return
     */
    public int calPouints(String[] ops) {

        Stack<Integer> opsStack = new Stack<>();
        for (String op : ops) {
            if ("+".equals(op)){
                int i1 = opsStack.pop();
                int i2 = opsStack.peek();

                opsStack.push(i1);
                opsStack.push(i1+i2);
            }else if("D".equals(op)){
                opsStack.push(opsStack.peek()*2);
            }else if ("C".equals(op)){
                opsStack.pop();
            }
            opsStack.push(Integer.parseInt(op));
        }
        int sum = 0;
        for (Integer integer : opsStack) {
            sum += integer;
        }
        return sum;
    }
}
